﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _templateprojectedit : JumbotCms.API.AdminCenter
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Admin_Load("0000", "html");
            id = Str2Str(q("id"));
            if (!Page.IsPostBack)
            {
                doh.Reset();
                doh.SqlCmd = "SELECT [ID],[Title] FROM [jcms_normal_skins] ORDER BY Id asc";
                DataTable dtSkins = doh.GetDataTable();
                this.ddlSkinId.DataSource = dtSkins;
                this.ddlSkinId.DataTextField = "Title";
                this.ddlSkinId.DataValueField = "ID";
                this.ddlSkinId.DataBind();
                dtSkins.Clear();
                dtSkins.Dispose();
            }
            Jumbot.DataOper.WebFormHandler wh = new Jumbot.DataOper.WebFormHandler(doh, "jcms_normal_templateproject", btnSave);
            wh.AddBind(txtTitle, "Title", true);
            wh.AddBind(txtDir, "Dir", true);
            wh.AddBind(txtInfo, "Info", true);
            if (id == "0")
            {
                string isDef = "0";
                wh.AddBind(ref isDef, "IsDefault", true);
                wh.Mode = Jumbot.DataOper.OperationType.Add;
            }
            else
            {
                this.txtDir.Enabled = false;
                this.ddlSkinId.Enabled = false;
                wh.ConditionExpress = "id=" + id;
                wh.Mode = Jumbot.DataOper.OperationType.Modify;
            }
            wh.validator = chkForm;
            wh.AddOk += new EventHandler(add_ok);
            wh.ModifyOk += new EventHandler(save_ok);
        }
        protected void bind_ok(object sender, EventArgs e)
        {
        }
        protected bool chkForm()
        {
            if (!CheckFormUrl())
                return false;
            if (!Page.IsValid)
                return false;
            doh.Reset();
            doh.ConditionExpress = "Title=@title and id<>" + id;
            doh.AddConditionParameter("@title", txtTitle.Text);
            if (doh.Exist("jcms_normal_templateproject"))
            {
                FinalMessage("方案名重复!", "", 1);
                return false;
            }
            return true;
        }
        protected void add_ok(object sender, EventArgs e)
        {
            int _dafaultSkinId = Str2Int(this.ddlSkinId.SelectedValue);
            Jumbot.DataOper.DbOperEventArgs de = (Jumbot.DataOper.DbOperEventArgs)e;
            string _tempTitles = "站点首页";
            string _tempTypes = "system";
            string _tempStypes = "index";
            doh.Reset();
            doh.SqlCmd = "Select [Id],[Title],[Type] FROM [jcms_normal_modules] ORDER BY pid";
            DataTable dt = doh.GetDataTable();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                _tempTitles += "|" + dt.Rows[i]["Title"].ToString() + "频道页" +
                    "|" + dt.Rows[i]["Title"].ToString() + "列表页" +
                    "|" + dt.Rows[i]["Title"].ToString() + "内容页";
                _tempTypes += "|" + dt.Rows[i]["Type"].ToString().ToLower() +
                    "|" + dt.Rows[i]["Type"].ToString().ToLower() +
                    "|" + dt.Rows[i]["Type"].ToString().ToLower();
                _tempStypes += "|channel|class|content";
            }
            string[] _tempTitle = _tempTitles.Split('|');
            string[] _tempType = _tempTypes.Split('|');
            string[] _tempStype = _tempStypes.Split('|');
            for (int i = 0; i < _tempTitle.Length; i++)
            {
                doh.Reset();
                doh.AddFieldItem("Title", this.txtTitle.Text + _tempTitle[i]);
                doh.AddFieldItem("Type", _tempType[i]);
                doh.AddFieldItem("sType", _tempStype[i]);
                doh.AddFieldItem("Source", _tempType[i] + "_" + _tempStype[i] + ".htm");
                doh.AddFieldItem("pId", de.id);
                doh.AddFieldItem("IsDefault", 0);
                doh.AddFieldItem("SkinId", _dafaultSkinId);
                doh.Insert("jcms_normal_template");
            }
            FinalMessage("成功保存", site.Dir + "admin/close.htm", 0);
        }
        protected void save_ok(object sender, EventArgs e)
        {
            FinalMessage("成功保存", site.Dir + "admin/close.htm", 0);
        }
    }
}
